//
//  MBProgressHUD+MJ.m
//
//  Created by mj on 13-4-18.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MBProgressHUD+MJ.h"
#import <lottie-ios/Lottie/LOTAnimationView.h>

@implementation MBProgressHUD (MJ)
#pragma mark 显示信息
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
{
    if (view == nil) view = [[UIApplication sharedApplication].windows firstObject];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = text;
    if (!icon) {
        hud.labelFont = [UIFont boldSystemFontOfSize:15.0];
    }
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    if (icon) {
        
        // 设置图片
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
    }
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
        
    hud.userInteractionEnabled = NO;
    
    // 0.7秒之后再消失
    [hud hide:YES afterDelay:2.0];
}

#pragma mark 显示错误信息
+ (void)showError:(NSString *)error toView:(UIView *)view{
    [self show:error icon:@"error.png" view:view];
}

+ (void)showSuccess:(NSString *)success toView:(UIView *)view
{
    [self show:success icon:@"success.png" view:view];
}

+ (void)showText:(NSString *)text toView:(UIView *)view
{
    [self show:text icon:nil view:view];
}

#pragma mark 显示一些信息
+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view netWork:(BOOL)isNetWork mask:(BOOL)isMask{
    if (view == nil) view = [[[UIApplication sharedApplication] keyWindow].subviews firstObject];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = message;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    hud.square = YES;
    
//    hud.mode = MBProgressHUDModeIndeterminate;
    
    //如果需要自定义的图片的话
    hud.mode = MBProgressHUDModeCustomView;
    
    if (Iphone4 || Iphone5) {
    
        hud.minSize = CGSizeMake(80, 80);
    }
    else{
    
        hud.minSize = CGSizeMake(100, 100);
    }
    
    //判断是否开启状态栏指示器
    [UIApplication sharedApplication].networkActivityIndicatorVisible = isNetWork;
    
    LOTAnimationView *rippleAnimationView = [LOTAnimationView animationNamed:@"loading"];
    rippleAnimationView.loopAnimation = true;
    [rippleAnimationView play];
    
    if (Iphone5 || Iphone4) {
        
        rippleAnimationView.frame = CGRectMake(0, 0, 60, 60);
    }
    else{
     
        rippleAnimationView.frame = CGRectMake(0, 0, 70, 70);
    }
    hud.customView = rippleAnimationView;
    
    return hud;
}

+ (void)showMessage:(NSString *)message toView:(UIView *)view
{
    [self showMessage:message toView:view netWork:NO mask:NO];
}

+ (void)showMessage:(NSString *)message toView:(UIView *)view mark:(BOOL)mark
{
    [self showMessage:message toView:view netWork:NO mask:mark];
}

+ (void)showSuccess:(NSString *)success
{
    [self showSuccess:success toView:nil];
}

+ (void)showError:(NSString *)error
{
    [self showError:error toView:nil];
}

+ (void)showText:(NSString *)text
{
    [self showText:text toView:nil];
}

+ (MBProgressHUD *)showMessage:(NSString *)message
{
    return [self showMessage:message toView:nil netWork:NO mask:NO];
}

+ (MBProgressHUD *)showMessage:(NSString *)message netWork:(BOOL)isNetWork
{
    return [self showMessage:message toView:nil netWork:YES mask:YES];
}

+ (BOOL)hideHUDForView:(UIView *)view
{
   return [self hideHUDForView:view animated:YES];
}

+ (void)hideHUD
{
    [self hideHUDForView:nil];
}

+ (void)hideHUDNoAnimation
{
    [self hideHUDForView:nil animated:NO];
}
@end
